<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 用户已领取优惠券
 */
namespace app\allwin\controller\api\v4;
use app\allwin\controller\api\Base;
use app\allwin\model\CouponUser as AllwinCouponUser;
use think\helper\Time;

class Couponuser extends Base{
       
    /**
     * 初始化当前应用管理员是不是联盟城市账户
     * @return void
     */
    public function initialize() {
        parent::initialize();
        $this->isUserAuth();
    }

     /**
     * 用户的优惠券
     **/
    public function index(){
        $param['types']   = $this->request->param('types/d',0);
        $param['page']    = $this->request->param('page/d',1);
        $param['signkey'] = $this->request->param('signkey');
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        //获取现在的时间 
        $times       = Time::today();
        $condition   = [];
        $condition[] = ['uid','=',$this->user->id];
        $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
        switch ($param['types']) {
            case 1:
                $condition[] = ['types','=',0];
                $condition[] = ['is_end','=',0];
                break;
            case 2:
                $condition[] = ['types','=',1];
                $condition[] = ['is_end','=',0];
                break; 
            case 3:
                $condition[] = ['is_end','=',1];
                break; 
            default:
                $condition[] = ['is_end','=',0];
                break;
        }
        $rel = AllwinCouponUser::where($condition)->order('id desc')->paginate(10,true);
        if($rel){
            $coupon = [];
            foreach ($rel as $key => $value) {
                $coupon[$key]['id']        = $value['id'];
                $coupon[$key]['types']     = $value['types'];
                $coupon[$key]['discount']  = $value['discount'];
                $coupon[$key]['store']     = $value->store->name;
                $coupon[$key]['store_id']  = $value->store->id;
                $coupon[$key]['name']      = $value['name'];
                $coupon[$key]['size']      = $value['size'];
                $coupon[$key]['user_size'] = $value['user_size'];
                $coupon[$key]['tips']      = $value['tips'];
                $coupon[$key]['price']     = $value['price'];
                $coupon[$key]['is_end']    = $value['is_end'];
                $coupon[$key]['state']     = $value['endtime'] > $times ? 0 : 2;
                $theme                     = $value['types'] ? 'yellow' : '';
                $coupon[$key]['theme']     = $value['is_end'] ? 'disabled' : $theme;
                $coupon[$key]['starttime'] = date('Y-m-d',$value['starttime']);
                $coupon[$key]['endtime']   = date('Y-m-d',$value['endtime']);
            }
            return json(['code'=>200,'msg' => "成功",'data' => $coupon]);
        }else{
            return json(['code'=>404,'msg' => "失败"]);
        }

    }
     /**
     * 读取单个优惠券信息
     **/
    public function getCoupon(){
        $param['id']      = $this->request->param('id/d');
        $param['signkey'] = $this->request->param('signkey');
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $coupon = AllwinCouponUser::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'uid' => $this->user->id])->find();
        $data['id']                  = $coupon['id'];
        $data['store_id']            = $coupon['store_id'];
        $data['types']               = $coupon['types'];
        $data['name']                = $coupon['name'];
        $data['size']                = $coupon['size'];
        $data['user_size']           = $coupon['user_size'];
        $data['price']               = $coupon['price'];
        $data['discount']            = $coupon['discount'];
        $data['howmuch']             = $coupon['howmuch'];
        $data['tips']                = $coupon['tips'];
        $data['is_end']              = $coupon['is_end'];
        $data['weekday']             = implode(',',weekdays(json_decode($coupon['weekday'])));
        $data['starttime']           = date('Y-m-d',$coupon['starttime']);
        $data['endtime']             = date('Y-m-d',$coupon['endtime']);
        $theme                       = $coupon['types'] ? 'yellow' : '';
        $data['theme']              = $coupon['is_end'] ? 'disabled' : $theme;
        $data['store']['id']         = $coupon->store['id'];
        $data['store']['name']       = $coupon->store['name'];
        $data['store']['img']        = $coupon->store['img']."?x-oss-process = style/w100";
        $data['store']['address']    = $coupon->store['address'];
        $data['store']['telphone']   = $coupon->store['telphone'];
        $data['store']['tips']       = $coupon->store['tips'];
        $data['store']['is_top']     = $coupon->store['is_top'];
        $data['store']['tags']       = explode(',',$coupon->store['tags']);
        return enjson(200,'成功',$data);
    }
}